﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections.ObjectModel;
using System.IO;
using Elentok.Common;
using AMail.Common.Interfaces.Data;
using AMail.Common.Interfaces.Services;

namespace AMail.Modules.Data.Unittests
{
  static class UnittestHelper
  {
    //public ReadOnlyCollection<string> DemoEmailNames { get; private set; }
    
    //public UnittestHelper()
    //{
    //  List<string> demoEmailNames = new List<string>();
    //  string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
    //  foreach (string name in names)
    //  {
    //    if (name.Contains(".DemoEmails"))
    //      demoEmailNames.Add(name);
    //  }

    //  this.DemoEmailNames = demoEmailNames.AsReadOnly();
    //}

    //public byte[] GetDemoEmail(int index)
    //{
    //  string name = this.DemoEmailNames[index];
    //  using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
    //  {
    //    return stream.ReadAllBytes();
    //  }
    //}

    private static string tempArchiveFileName;

    public static string TempArchiveFileName
    {
      get
      {
        if (string.IsNullOrEmpty(tempArchiveFileName))
          tempArchiveFileName = Environment.ExpandEnvironmentVariables("%TEMP%\\Unittest.amail");

        return tempArchiveFileName;
      }
    }

    public static void DeleteTempArchive()
    {
      if (File.Exists(TempArchiveFileName))
        File.Delete(TempArchiveFileName);
    }

    public static IMailArchive CreateTempArchive(this IDataService service)
    {
      DeleteTempArchive();

      IMailArchive archive = 
        service.CreateArchive(UnittestHelper.TempArchiveFileName, UnittestHelper.TempArchivePassword);

      return archive;
    }

    public static IMailArchive OpenTempArchive(this IDataService service)
    {
      IMailArchive archive =
        service.CreateArchive(UnittestHelper.TempArchiveFileName, UnittestHelper.TempArchivePassword);

      return archive;
    }

    public static string TempArchivePassword
    {
      get { return "123"; }
    }

  }
}
